package main.canvas.components;

import main.canvas.CanvasComponent;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ImageComponent extends CanvasComponent {

    private String filename;

    public ImageComponent(String filename) {
        this.filename = filename;
    }

    @Override
    public void draw(Graphics2D g) {
        BufferedImage image;
        try {
            image = ImageIO.read(ImageComponent.class.getClassLoader().getResourceAsStream(filename));
        } catch (IOException e) {
            throw new RuntimeException("Can not read image: " + filename);
        }
        g.drawImage(image, x, y, width, height, null);
    }
}
